
import JsEncrypt from './jsencrypt.min.js';
class JsEncryptNew extends JsEncrypt {
    constructor() {
        super();
        this.b64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
        this.b64pad = '=';
        this.BI_RM = '0123456789abcdefghijklmnopqrstuvwxyz';
    }

    int2char = function(n) {
        return this.BI_RM.charAt(n);
    };
    b64tohex = function(s) {
        var ret = '';
        var i;
        var k = 0; // b64 state, 0-3
        var slop = 0;
        for (i = 0; i < s.length; ++i) {
            if (s.charAt(i) === this.b64pad) {
                break;
            }
            var v = this.b64map.indexOf(s.charAt(i));
            if (v < 0) {
                continue;
            }
            if (k === 0) {
                ret += this.int2char(v >> 2);
                slop = v & 3;
                k = 1;
            } else if (k === 1) {
                ret += this.int2char((slop << 2) | (v >> 4));
                slop = v & 0xf;
                k = 2;
            } else if (k === 2) {
                ret += this.int2char(slop);
                ret += this.int2char(v >> 2);
                slop = v & 3;
                k = 3;
            } else {
                ret += this.int2char((slop << 2) | (v >> 4));
                ret += this.int2char(v & 0xf);
                k = 0;
            }
        }
        if (k === 1) {
            ret += this.int2char(slop << 2);
        }
        return ret;
    };
    hex2b64 = function(h) {
        var i;
        var c;
        var ret = '';
        for (i = 0; i + 3 <= h.length; i += 3) {
            c = parseInt(h.substring(i, i + 3), 16);
            ret += this.b64map.charAt(c >> 6) + this.b64map.charAt(c & 63);
        }
        if (i + 1 === h.length) {
            c = parseInt(h.substring(i, i + 1), 16);
            ret += this.b64map.charAt(c << 2);
        } else if (i + 2 === h.length) {
            c = parseInt(h.substring(i, i + 2), 16);
            ret += this.b64map.charAt(c >> 2) + this.b64map.charAt((c & 3) << 4);
        }
        while ((ret.length & 3) > 0) {
            ret += this.b64pad;
        }
        return ret;
    };
    // 分段加密
    encryptLong = function(string) {
        var k = this.getKey();
        // var maxLength = (((k.n.bitLength() + 7) >> 3) - 11);//117
        try {
            // var lt = '';
            var ct = '';
            // RSA每次加密117bytes，需要辅助方法判断字符串截取位置
            // 1.获取字符串截取点
            var bytes = [];
            bytes.push(0);
            var byteNo = 0;
            var len, c;
            len = string.length;
            var temp = 0;
            for (var i = 0; i < len; i++) {
                c = string.charCodeAt(i);
                if (c >= 0x010000 && c <= 0x10FFFF) {
                    byteNo += 4;
                } else if (c >= 0x000800 && c <= 0x00FFFF) {
                    byteNo += 3;
                } else if (c >= 0x000080 && c <= 0x0007FF) {
                    byteNo += 2;
                } else {
                    byteNo += 1;
                }
                if ((byteNo % 117) >= 114 || (byteNo % 117) === 0) {
                    if (byteNo - temp >= 114) {
                        bytes.push(i);
                        temp = byteNo;
                    }
                }
            }
            // 2.截取字符串并分段加密
            if (bytes.length > 1) {
                for (let i = 0; i < bytes.length - 1; i++) {
                    var str;
                    if (i === 0) {
                        str = string.substring(0, bytes[i + 1] + 1);
                    } else {
                        str = string.substring(bytes[i] + 1, bytes[i + 1] + 1);
                    }
                    var t1 = k.encrypt(str);
                    ct += t1;
                }
                ;
                if (bytes[bytes.length - 1] !== string.length - 1) {
                    var lastStr = string.substring(bytes[bytes.length - 1] + 1);
                    ct += k.encrypt(lastStr);
                    // debugger;
                }
                return this.hex2b64(ct);
            }
            var t = k.encrypt(string);
            var y = this.hex2b64(t);
            return y;

        } catch (ex) {
            return false;
        }
    };
    // 分段解密长字符串
    decryptLong = function(text) {
        // Return the decrypted string.
        // console.log(this);
        var k = this.getKey();
        var maxLength = ((k.n.bitLength() + 7) >> 3);
        try {
            var str = this.b64tohex(text);
            // var b=hex2Bytes(str);

            var inputLen = str.length;

            var ct = '';

            if (inputLen > maxLength) {

                var lt = str.match(/.{1,256}/g);
                lt.forEach(function(entry) {
                    var t1 = k.decrypt(entry);
                    ct += t1;
                });
                return ct;
            }
            var y = k.decrypt(this.b64tohex(text));
            return y;
        } catch (ex) {
            return false;
        }
    };
}

const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDULe+qzfRXJt704YCxsEQC+Ql8XqaundGYjtRdkJB+Yu/NYYgECM0JFiyWg0J/RHgxiW5ulv1m/k//T0VATom8CsAml/pQ2Q7xfr/Jx54yZA7lFcXWsYVI19Y0BYL/SEf2rVH8cMzOeFljVFZ5+4CPn/5K9M+FhjnrIZd83wvrSQIDAQAB';
export const $encruption = (str, key = publicKey) => {
    const ENCRYPT = new JsEncryptNew();
    ENCRYPT.setPublicKey(key);
    return ENCRYPT.encryptLong(str.toString());
};
const privateKey = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANQt76rN9Fcm3vThgLGwRAL5CXxepq6d0ZiO1F2QkH5i781hiAQIzQkWLJaDQn9EeDGJbm6W/Wb+T/9PRUBOibwKwCaX+lDZDvF+v8nHnjJkDuUVxdaxhUjX1jQFgv9IR/atUfxwzM54WWNUVnn7gI+f/kr0z4WGOeshl3zfC+tJAgMBAAECgYB9v+/QAhny4PW4mkFEjhvwyxnFHO+ag6wpx1SzUy9fyFEpHsmW4oRqAztPxOFPPgwiB7psTo5GUSimPpvjXeigdiALu5h6jw752tkTwY6r8xJPJGbH4j6guZQ1Boog9sQZs1d89EicPg8DVZ7EtSk+Utn5K1T5CCrOYEzPq836AQJBAPKb++LhV/KvzCioYCY6tg8ys99vgS7MwK/rssJAhxGxtvKo+CrorB3cNYTva60TDngY1POkqmia53ipBcSRuUECQQDf4/wf8UK4yqN7aqVi+CKY2dTNV/JqSOM3J6HZpRZFpZijchDX79K3iDWrRSm2S5PcQ1D3xjqoVEnyAR/eKmgJAkEAwqqaoF/EW/znSjBghgxSDTvXzI8CDvVIH2B2XRu9xVrduzQNfRCZGpSZdQNPWN6Tm5YBOUaFXc39sXmeIS30gQJBAMPX2qBaWwHoB68ab4cuBJ1t1k61ApDkJxUE7vfCB8RugSFf0soAt3LhjAjAHCeg3rsKi1XhqUe7S1+MikK9F3kCQHCCFDRcL5frcgwuAeTdWjl7/UvDbBHLFmR/RJlhUKRvOBItD6+4kPtxhpxkEzKnO+gAe5HhP96CkNmM9tiRRew=';
export const $decryption = (str, key = privateKey) => {
    const ENCRYPT = new JsEncryptNew();
    ENCRYPT.setPrivateKey(key);
    return ENCRYPT.decryptLong(str.toString());
};


const publicKey1 = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEX4BC7IdzuFswt3PrzwNQmBS6l2jM7sE8EnalFOBwRZtv3h6punClcUYSjLjuEaTHEvArajKN3SKN7ccRPRtyQmhf6ghaV+8PoDb/PrgVZ/+jzy2feZXIcQKBnqSJZtTZZPG/L8kRNOYVrMPeT/daf8qW7oWl2Cmi1iY6XmHV9wIDAQAB';
export const $encruption1 = (str, key = publicKey1) => {
    const ENCRYPT = new JsEncryptNew();
    ENCRYPT.setPublicKey(key);
    return ENCRYPT.encryptLong(str.toString());
};
const privateKey1 = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMRfgELsh3O4WzC3c+vPA1CYFLqXaMzuwTwSdqUU4HBFm2/eHqm6cKVxRhKMuO4RpMcS8CtqMo3dIo3txxE9G3JCaF/qCFpX7w+gNv8+uBVn/6PPLZ95lchxAoGepIlm1Nlk8b8vyRE05hWsw95P91p/ypbuhaXYKaLWJjpeYdX3AgMBAAECgYBXyN18jWIr8AEL8SaoBp3TvHifxSm7WwwU/6f8Oi3u4p8Ozjvk4gyTWOTbS/8rpcWEnMmyjsHUu0NEMURpR2jpzQTss+wFWep1HBj7TDOT5RCoR4EulLlxat9prmUz66oQA9hBYTxwW05XKtJE0j6vi5AxT+R0NPZ4JsOsGWc/QQJBAPJsamZpCNqYPf+oaq+jf4NFcWiIp4HDBH5QpX6bTR0wCCBW/xqn7UHTcBCdLtpqBi0Vdt4coJ2OT4ExQRKBrdcCQQDPXt4zxqngQYzTyaEAF19EGUyfYHhOJ82nPidlkyngrpSpTwYmJyEsSuPQp47V2+L/HMlPwTaRvvDlKcv63tThAkEAh7LyWEMhRqODyPEwTwLZZNThZ59fDH7qq/5etSEfqF/znI62G+i2IWvGxjYGPRKuRFxIfopNygQYMvwsLbzTXwJAIdzuECpq5zINGzCb1/X5Gpe/CW+pdanOTXc6271yYzxfDl7XNDfDlQYNua8DBi1IHjzHKvz0Okyh7eorYyYsQQJAVxkLcXG+Kj5sLT65YtUWYFRDNGEqcEY2raWh6ziqh0QTTZbPnvn4t5pPFI5quN7znbe3ijHHuG/n9d3r00ezRQ==';
export const $decryption1 = (str, key = privateKey1) => {
    const ENCRYPT = new JsEncryptNew();
    ENCRYPT.setPrivateKey(key);
    return ENCRYPT.decryptLong(str.toString());
};